package com.mail.server.service;

import cn.hutool.core.collection.CollUtil;
import com.mail.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.mail.dubbo.api.SmsFlashPromotionSessionApi;
import com.mail.model.db.SmsFlashPromotionProductRelation;
import com.mail.model.db.SmsFlashPromotionSession;
import com.mail.model.vo.SmsFlashPromotionSessionTimeVo;
import com.mail.model.vo.SmsFlashPromotionSessionVo;
import com.mail.server.common.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @author yan
 * @date 2022/06/21 18:46
 **/
@Service
public class SmsFlashPromotionSessionService {
    @DubboReference
    private SmsFlashPromotionSessionApi smsFlashPromotionSessionApi;

    public R create(SmsFlashPromotionSessionTimeVo session) {
        SmsFlashPromotionSession smsFlashPromotionSession = getSession(session);
        smsFlashPromotionSessionApi.create(smsFlashPromotionSession);
        return R.success("");
    }

    public R delete(Long id) {
        smsFlashPromotionSessionApi.delete(id);
        return R.success("");
    }

    public R list() {
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionApi.list();
        List<SmsFlashPromotionSessionTimeVo> timeVos = new ArrayList<>();
        for (SmsFlashPromotionSession session : list) {
            SmsFlashPromotionSessionTimeVo extracted = extracted(session);
            timeVos.add(extracted);
        }
        return R.success(timeVos);
    }

    @DubboReference
    private SmsFlashPromotionProductRelationApi smsFlashPromotionProductRelationApi;

    public R selectList(Long flashPromotionId) {
        List<Long> list = smsFlashPromotionProductRelationApi.selectList(flashPromotionId);
        if (CollUtil.isEmpty(list)) {
            return R.success(null);
        }
        List<SmsFlashPromotionSession> promotionSessionList = smsFlashPromotionSessionApi.listByIds(list);
        List<SmsFlashPromotionSessionVo> voList = new ArrayList<>();
        for (SmsFlashPromotionSession smsFlashPromotionSession : promotionSessionList) {
            SmsFlashPromotionSessionVo smsFlashPromotionSessionVo = new SmsFlashPromotionSessionVo();
            SmsFlashPromotionSessionTimeVo extracted = extracted(smsFlashPromotionSession);
            BeanUtils.copyProperties(extracted, smsFlashPromotionSessionVo);
            Integer count =smsFlashPromotionProductRelationApi.getProductCount(flashPromotionId,smsFlashPromotionSession.getId());
            //将count数据封装到vo中
            smsFlashPromotionSessionVo.setProductCount(count);
            voList.add(smsFlashPromotionSessionVo);
        }
        return R.success(voList);
    }

    private SmsFlashPromotionSessionTimeVo extracted(SmsFlashPromotionSession session) {
        SmsFlashPromotionSessionTimeVo timeVo = new SmsFlashPromotionSessionTimeVo();
        LocalTime startTime = session.getStartTime();
        LocalDateTime startTimes =startTime.atDate(LocalDate.of(1970, 1, 1));
        LocalTime endTime = session.getEndTime();
        LocalDateTime endTimes =endTime.atDate(LocalDate.of(1970, 1, 1));
        BeanUtils.copyProperties(session, timeVo);
        timeVo.setStartTime(startTimes);
        timeVo.setEndTime(endTimes);
        return timeVo;
    }

    public R updateStatus(Long id, Long status) {
        smsFlashPromotionSessionApi.updateStatus(id, status);
        return R.success("");
    }

    public R update(Long id, SmsFlashPromotionSessionTimeVo session) {
        SmsFlashPromotionSession smsFlashPromotionSession = getSession(session);
        smsFlashPromotionSessionApi.update(id, smsFlashPromotionSession);
        return R.success(null);
    }

    /**将LocalDateTime转成LocalTime
     * @param session
     * @return
     */
    private SmsFlashPromotionSession getSession(SmsFlashPromotionSessionTimeVo session) {
        LocalDateTime startTime = session.getStartTime();
        LocalDateTime endTime = session.getEndTime();
        LocalTime startTimes=startTime.toLocalTime();
        LocalTime endTimes = endTime.toLocalTime();
        SmsFlashPromotionSession smsFlashPromotionSession=new SmsFlashPromotionSession();
        BeanUtils.copyProperties(session,smsFlashPromotionSession);
        smsFlashPromotionSession.setStartTime(startTimes);
        smsFlashPromotionSession.setEndTime(endTimes);
        return smsFlashPromotionSession;
    }

    public R getFlashSession(Long id) {
        SmsFlashPromotionSession session = smsFlashPromotionSessionApi.getFlashSession(id);
        return R.success(session);
    }
}
